package io.renren.modules.wmstate.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl;
import io.renren.modules.wmstate.entity.DataListResponse;
import io.renren.mqtt.MqttConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.wmstate.dao.WeldingmachineStateDao;
import io.renren.modules.wmstate.entity.WeldingmachineStateEntity;
import io.renren.modules.wmstate.service.WeldingmachineStateService;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;


@Service("weldingmachineStateService")
public class WeldingmachineStateServiceImpl extends ServiceImpl<WeldingmachineStateDao, WeldingmachineStateEntity> implements WeldingmachineStateService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WeldingmachineStateEntity> page = this.page(
                new Query<WeldingmachineStateEntity>().getPage(params),
                new QueryWrapper<WeldingmachineStateEntity>()
        );

        return new PageUtils(page);
    }


    private static final String MACHINE_LIST_URL = "http://10.142.3.129:8080/system/machine/list";

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String getMachineList(String collModelCode) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
//        String token = getWmToken().toString();
        headers.setBearerAuth(getWmToken().toString());

        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(MACHINE_LIST_URL)
                .queryParam("collModelCode", collModelCode);

        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);

        //System.out.println(response.getBody());
        return response.getBody();
    }



    private static final String DATA_LIST_URL = "http://10.142.3.129:8080/system/point/list";
    @Override
    public String listData(String gwsn,String startDate) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
//        String token = getWmToken().toString();
        headers.setBearerAuth(getWmToken().toString());

        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(DATA_LIST_URL)
                .queryParam("gwsn", gwsn)
                .queryParam("startDate",startDate)
                .queryParam("pageSize",10)
                .queryParam("pageNum",1);

        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);

        //System.out.println(response.getBody());
        return response.getBody();
    }


    //持久化mq推送的消息
    @Autowired
    WeldingmachineStateDao weldingmachineStateDao;
    @Override
    public int postPointData(MqttConfiguration.PointData pointData) {
        return weldingmachineStateDao.postPointData(pointData);
    }

    @Override
    public String countStatus() {
        System.out.println(weldingmachineStateDao.countStatus());
        return weldingmachineStateDao.countStatus();
    }

//    @Override
//    public List<DataListResponse.DataList> getDataList(String gwsn, String startDate) {
//        return null;
//    }

    //1.获取token
    public Object getWmToken() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        // 获取token
//        UsersServiceImpl.ReHeader reHeader = new UsersServiceImpl.ReHeader();
        WelderinfoServiceImpl.ReHeader reHeader = new WelderinfoServiceImpl.ReHeader();
        reHeader.setKey("cmhi2023");
        String url = "http://10.142.3.129:8080/getOpenApiToken";
        String result = restTemplate.postForObject(url,reHeader, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(result);
        String token = String.valueOf(jsonNode.get("data").get("token")).replace("\"","");
        return token.toString();
    }

}